package com.erp.erp_mask.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.erp.erp_mask.mapper.TManufactureplanMapper;
import com.erp.erp_mask.mapper.TPermissionsMapper;
import com.erp.erp_mask.mapper.TProcessMapper;
import com.erp.erp_mask.mapper.TSalesordersMapper;
import com.erp.erp_mask.pojo.TPermissions;
import com.erp.erp_mask.pojo.TProcess;
import com.erp.erp_mask.service.TManufactureplanService;
import com.erp.erp_mask.service.TProStateService;
import com.erp.erp_mask.service.TSalesordersService;
import com.erp.erp_mask.vo.OrderData;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @BelongsProject: erp
 * @BelongsPackage: com.erp.erp_mask.controller
 * @Author: wang
 * @CreateTime: 2024-05-20  17:12
 * @Description: TODO
 * @Version: 1.0
 * @effect:
 */
@RestController
@RequestMapping("/pro")
public class TManufactureplanController {
    @Resource
    private TPermissionsMapper tPermissionsMapper;
    @Resource
    private TManufactureplanService tManufactureplanService;
    @Resource
    private TProStateService tProStateService;
    @Resource
    private TManufactureplanMapper tManufactureplanMapper;
    @Resource
    private TProcessMapper tProcessMapper;

    @GetMapping("/index")
    private ModelAndView index() {
        return new ModelAndView("pro/proindex");
    }

    //导航栏  生产管理
    @GetMapping("/searchbar")
    public Result searchbar() {
        LambdaQueryWrapper<TPermissions> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TPermissions::getParentId, 35);
        return Result.success(tPermissionsMapper.selectList(wrapper));
    }

    //生产计划
    @GetMapping("/proplan")
    public ModelAndView proindex() {
        return new ModelAndView("/pro/proplan");
    }
    @GetMapping("/search")
    public Result search(Integer prostateid,String startdate,String overdate) {
        return Result.success(tManufactureplanMapper.search(prostateid,startdate,overdate));
    }
    @GetMapping("/stateSearch")
    public Result stateSearch() {
        return Result.success(tProStateService.list());
    }
    @GetMapping("/orderdatalist")
    public Result orderdatalist(Integer cid){
        return Result.success(tManufactureplanMapper.ordersearchlist(cid));
    }
    //计划添加
    @PostMapping("/planadd")
    @Transactional
    public Result planadd(@RequestBody OrderData orderData){
        try{
            System.out.println(orderData);
            tManufactureplanMapper.planaddlist(orderData);
            tManufactureplanMapper.planedit(orderData.getOrdcomId());
            return Result.success("成功!");
        }catch (Exception e){
            return Result.fail("失败!");
        }
    }
    @GetMapping("/bomidsearch")
    public Integer bomidsearch(String name){
        return tManufactureplanMapper.bomidsearch(name);
    }
    @PostMapping("/addprocess")
    //添加加工单
    public Result addprocess(@RequestBody TProcess process){
        try{
            tProcessMapper.addprocess(process);
            return Result.success("已加入加工单!");
        }catch (Exception e){
            return Result.fail("未能成功加入加工单!");
        }
    }
    @GetMapping("/stateupdate")
    public void stateupdate(Integer id){
        System.out.println(id);
        tProcessMapper.stateupdate(id);
    }
}
